# -*- python -*-
# ex: set syntax=python:

# Copyright 2013 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

# This is the buildmaster config file for the 'pushMessagingCanary' bot. It must
# be installed as 'master.cfg' in your buildmaster's base directory
# (although the filename can be changed with the --basedir option to
# 'mktap buildbot master').

# It has one job: define a dictionary named BuildmasterConfig. This
# dictionary has a variety of keys to control different aspects of the
# buildmaster. They are documented in docs/config.xhtml .

from buildbot.scheduler import Scheduler

from common import chromium_utils

from master import gitiles_poller
from master import master_utils
from master import slaves_list
from master.factory import chromium_factory
from buildbot.status.mail import MailNotifier

import config
import master_site_config

ActiveMaster = master_site_config.PushCanary

MAIL_NOTIFIER = MailNotifier(fromaddr="petewil@google.com",
                             sendToInterestedUsers=False,
                             extraRecipients=['petewil@google.com'],
                             mode='all')

PB_LISTENER = False
GIT_POLLER = True
STATUS_PUSH = False


# This is the dictionary that the buildmaster pays attention to. We also use
# a shorter alias to save typing.
c = BuildmasterConfig = {}

# 'slavePortnum' defines the TCP port to listen on. This must match the value
# configured into the buildslaves (with their --master option)
c['slavePortnum'] = ActiveMaster.slave_port


####### CHANGESOURCES

tagComparator = None
if GIT_POLLER:
  master_poller = gitiles_poller.GitilesPoller(
      'https://chromium.googlesource.com/chromium/src', pollInterval=600)
  tagComparator = master_poller.comparator
  c['change_source'] = [master_poller]


####### SCHEDULERS

## configure the Schedulers
# Push Canary Scheduler
# TODO: remove mac and win builders, unused.
s_chromium_push_canary = Scheduler(name='push_canary',
                                    branch='master',
                                    treeStableTimer=60,
                                    builderNames=['Chromium Win Push Canary',
                                                  'Chromium Mac Push Canary',
                                                  'Chromium Linux Push Canary'])

c['schedulers'] = [s_chromium_push_canary]


####### BUILDERS

# buildbot/process/factory.py provides several BuildFactory classes you can
# start with, which implement build processes for common targets (GNU
# autoconf projects, CPAN perl modules, etc). The factory.BuildFactory is the
# base class, and is configured with a series of BuildSteps. When the build
# is run, the appropriate buildslave is told to execute each Step in turn.

# the first BuildStep is typically responsible for obtaining a copy of the
# sources. There are source-obtaining Steps in buildbot/process/step.py for
# CVS, SVN, and others.

builders = []

# ----------------------------------------------------------------------------
# FACTORIES

m = chromium_factory.ChromiumFactory('src/build', target_platform='win32')

m_webkit = chromium_factory.ChromiumFactory('src/webkit',
                                            target_platform='win32')

m_linux = chromium_factory.ChromiumFactory('src/out',
                                           target_platform='linux2')

m_mac = chromium_factory.ChromiumFactory('src/xcodebuild',
                                         target_platform='darwin')

f_chromium_win_push_canary = m.ChromiumFactory(
    tests=[])

f_chromium_mac_push_canary = m_mac.ChromiumFactory(
    tests=[])

f_chromium_linux_push_canary = m_linux.ChromiumFactory(
    tests=['push_canary_tests'], target='Debug')

# ----------------------------------------------------------------------------
# BUILDER DEFINITIONS

# The 'builders' list defines the Builders. Each one is configured with a
# dictionary, using the following keys:
#  name (required): the name used to describe this bilder
#  slavename (required): which slave to use, must appear in c['slaves']
#  builddir (required): which subdirectory to run the builder in
#  factory (required): a BuildFactory to define how the build is run
#  periodicBuildTime (optional): if set, force a build every N seconds
#  category (optional): it is not used in the normal 'buildbot' meaning. It is
#                       used by gatekeeper to determine which steps it should
#                       look for to close the tree.
#

b_chromium_win_push_canary = {
  'name': 'Chromium Win Push Canary',
  'builddir': 'chromium-win-push-canary',
  'factory': f_chromium_win_push_canary,
  'auto_reboot' : False,
}

b_chromium_mac_push_canary = {
  'name': 'Chromium Mac Push Canary',
  'builddir': 'chromium-mac-push-canary',
  'factory': f_chromium_mac_push_canary,
  'auto_reboot' : False,
}

b_chromium_linux_push_canary = {
  'name': 'Chromium Linux Push Canary',
  'builddir': 'chromium-linux-push-canary',
  'factory': f_chromium_linux_push_canary,
  'auto_reboot' : False,
}

c['builders'] = [b_chromium_win_push_canary,
                 b_chromium_mac_push_canary,
                 b_chromium_linux_push_canary]

# Associate the slaves to the builders. The configuration is in slaves.cfg.
slaves = slaves_list.SlavesList('slaves.cfg', 'PushCanary')
for builder in c['builders']:
  builder['slavenames'] = slaves.GetSlavesName(builder=builder['name'])


####### BUILDSLAVES

# The 'slaves' list defines the set of allowable buildslaves. List all the
# slaves registered to a builder. Remove dupes.
c['slaves'] = master_utils.AutoSetupSlaves(c['builders'],
                                           config.Master.GetBotPassword())

# Make sure everything works together.
master_utils.VerifySetup(c, slaves)


####### STATUS TARGETS


# The 'buildbotURL' string should point to the location where the buildbot's
# internal web server (usually the html.Waterfall page) is visible. This
# typically uses the port number set in the Waterfall 'status' entry, but
# with an externally-visible host name which the buildbot cannot figure out
# without some help.

# Must come before AutoSetupMaster().
c['buildbotURL'] = ActiveMaster.buildbot_url

# Adds common status and tools to this master.
master_utils.AutoSetupMaster(c, ActiveMaster, MAIL_NOTIFIER,
                             order_console_by_time=True,
                             tagComparator=tagComparator)

# Add more.

if PB_LISTENER:
  from buildbot.status import client
  c['status'].append(client.PBListener(9988))

# Local event push.
if STATUS_PUSH:
  from buildbot.status.status_push import HttpStatusPush
  # Hint: use a dummy web server to inspect the packets being sent.
  c['status'].append(HttpStatusPush('http://localhost:8000/status-listener'))


####### PROJECT IDENTITY

# the 'projectName' string will be used to describe the project that this
# buildbot is working on. For example, it is used as the title of the
# waterfall HTML page. The 'projectURL' string will be used to provide a link
# from buildbot HTML pages to your project's home page.

c['projectName'] = ActiveMaster.project_name
c['projectURL'] = config.Master.project_url
c['title'] = "Chromium PushCanary"
